﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.IO;
using KillParser;
using Killboard.Models;


namespace Killboard.Tests
{
    /// <summary>
    /// Summary description for KillmailTests
    /// </summary>
    [TestClass]
    public class KillmailTests
    {
        public KillmailTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void CreateObject()
        {
            KillMail km = new KillMail();

            Assert.AreNotEqual(km, null);
        }

        [TestMethod]
        public void TestEmptyParse()
        {
            KillMail km = new KillMail();

            try
            {
                km.Parse();
            }
            catch (BadKillException bke)
            {
                Assert.AreEqual(bke.Message, "Kill text is empty.");
            }
        }

        [TestMethod]
        public void TestWithKillText()
        {
            Assembly a = Assembly.GetExecutingAssembly();

            Stream s = a.GetManifestResourceStream("Killboard.Tests.KillMail.txt");
            TextReader tr = new StreamReader(s);

            KillMail km = new KillMail();
            km.Parse(tr);
            
        }

        [TestMethod]
        public void TestValidKillDate()
        {
            //"Bad Killdate"

            KillMail km = new KillMail();

            km.Parse("1968.12.05 11:00:05");

            Assert.AreEqual(km.KillTimeUTC, new DateTime(1968, 12, 5, 11, 00, 05));

 
        }
        [TestMethod]
        [ExpectedException(typeof(BadKillException))]
        public void TestInvalidKillDate()
        {

            KillMail km = new KillMail();
            km.Parse("2009.02.31 11:15:05");
            
        }

        [TestMethod]
        [ExpectedException(typeof(BadKillException))]
        public void TestInvalidKillTime()
        {

            KillMail km = new KillMail();
            km.Parse("2009.01.11 26:15:05");

        }

        [TestMethod]
        public void ItemCount()
        {
            Assembly a = Assembly.GetExecutingAssembly();

            Stream s = a.GetManifestResourceStream("Killboard.Tests.KillMail.txt");
            TextReader tr = new StreamReader(s);

            KillMail km = new KillMail();
            km.Parse(tr);

            Assert.AreEqual(20, km.ItemCasualties.Count);

        }

        [TestMethod]
        public void TestWithKillText2()
        {
            Assembly a = Assembly.GetExecutingAssembly();

            Stream s = a.GetManifestResourceStream("Killboard.Tests.Killmail2.txt");
            TextReader tr = new StreamReader(s);

            KillMail km = new KillMail();
            km.Parse(tr);

        }

        [TestMethod]
        public void Calendartest()
        {

            DateTime testDate = DateTime.Now;

            System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("en-US");
            int week = myCI.Calendar.GetWeekOfYear(testDate, System.Globalization.CalendarWeekRule.FirstFourDayWeek,
            System.DayOfWeek.Sunday);


            DateTime result = ekbCalendar.GetBeginningDate(week, testDate.Year);

            int testWeek = myCI.Calendar.GetWeekOfYear(result, System.Globalization.CalendarWeekRule.FirstFourDayWeek,
            System.DayOfWeek.Sunday);

            Assert.AreEqual(week, testWeek);


            

        }

    }
}
